<html>
<head><meta charset="utf-8"><title>PartialEq and match · general · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/index.html">general</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/PartialEq.20and.20match.html">PartialEq and match</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="239611319"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/PartialEq%20and%20match/near/239611319" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Odd-Harald Myhren <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/PartialEq.20and.20match.html#239611319">(May 20 2021 at 15:29)</a>:</h4>
<p>Is it possible to get the following working? To me this feels like an inconsistency:</p>
<div class="codehilite" data-code-language="Rust"><pre><span></span><code><span class="cp">#[derive(Copy, Clone)]</span><span class="w"></span>
<span class="k">pub</span><span class="w"> </span><span class="k">enum</span> <span class="nc">Button</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="n">Left</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">1</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Right</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">2</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Down</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">4</span><span class="p">,</span><span class="w"></span>
<span class="w">    </span><span class="n">Up</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="mi">8</span><span class="p">,</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">impl</span><span class="w"> </span><span class="nb">PartialEq</span><span class="o">&lt;</span><span class="n">Button</span><span class="o">&gt;</span><span class="w"> </span><span class="k">for</span><span class="w"> </span><span class="kt">u16</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">fn</span> <span class="nf">eq</span><span class="p">(</span><span class="o">&amp;</span><span class="bp">self</span><span class="p">,</span><span class="w"> </span><span class="n">other</span>: <span class="kp">&amp;</span><span class="nc">Button</span><span class="p">)</span><span class="w"> </span>-&gt; <span class="kt">bool</span> <span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="o">*</span><span class="bp">self</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="o">*</span><span class="n">other</span><span class="w"> </span><span class="k">as</span><span class="w"> </span><span class="kt">u16</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">main</span><span class="p">()</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">if</span><span class="w"> </span><span class="mi">56</span><span class="k">u16</span><span class="w"> </span><span class="o">==</span><span class="w"> </span><span class="n">Button</span>::<span class="n">Left</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="fm">println!</span><span class="p">(</span><span class="s">"Funny"</span><span class="p">);</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>

<span class="w">    </span><span class="k">match</span><span class="w"> </span><span class="mi">56</span><span class="k">u16</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Button</span>::<span class="n">Left</span><span class="w"> </span><span class="o">=&gt;</span><span class="w"> </span><span class="p">(),</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</code></pre></div>
<p>The match statement doesn't work, getting the following error message:</p>
<div class="codehilite" data-code-language="Bash"><pre><span></span><code>error<span class="o">[</span>E0308<span class="o">]</span>: mismatched types
  --&gt; src/main.rs:21:9
   <span class="p">|</span>
<span class="m">3</span>  <span class="p">|</span>     <span class="nv">Left</span> <span class="o">=</span> <span class="m">1</span>,
   <span class="p">|</span>     -------- unit variant defined here
...
<span class="m">20</span> <span class="p">|</span>     match 56u16 <span class="o">{</span>
   <span class="p">|</span>           ----- this expression has <span class="nb">type</span> <span class="sb">`</span>u16<span class="sb">`</span>
<span class="m">21</span> <span class="p">|</span>         Button::Left <span class="o">=</span>&gt; <span class="o">()</span>,
   <span class="p">|</span>         ^^^^^^^^^^^^ expected <span class="sb">`</span>u16<span class="sb">`</span>, found enum <span class="sb">`</span>Button<span class="sb">`</span>
</code></pre></div>



<a name="239621608"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/PartialEq%20and%20match/near/239621608" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/PartialEq.20and.20match.html#239621608">(May 20 2021 at 16:35)</a>:</h4>
<p><a href="https://stackoverflow.com/a/57578431/155423">https://stackoverflow.com/a/57578431/155423</a></p>



<a name="239621836"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/PartialEq%20and%20match/near/239621836" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/PartialEq.20and.20match.html#239621836">(May 20 2021 at 16:37)</a>:</h4>
<p>Notably, matching isnt just glorified equality checks</p>



<a name="239622013"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/122651-general/topic/PartialEq%20and%20match/near/239622013" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/122651-general/topic/PartialEq.20and.20match.html#239622013">(May 20 2021 at 16:38)</a>:</h4>
<p><a href="https://stackoverflow.com/a/43211909/155423">https://stackoverflow.com/a/43211909/155423</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>